namespace Vanilla.Tools.Filer

import Vanilla.UI
import Vanilla

import System.Threading

// FILE INFO WINDOW
class FileInfoWindow (Window):
	override MaxSize as Size:
		get:
			return MinSize
	
	def constructor (f as File):
		super(f.Name)
		c = Column(self)
		Label(c, "Type: ${f.Type}")
		
		inh = "Inherited types:"
		for i in f.Type.InheritedTypes:
			inh += " ${i}"
		Label(c, inh)
		
		Label(c, "Path: ${f.Path}")

// ICON BOX
class IconBox (Box):
	override MinSize as Size:
		get:
			return Size(200,200)

	def constructor (p as Box):
		super(p)
		
	Scroller as VScroller = null
	
	protected def EnableScroller ():
		Scroller = VScroller(self, Scroll)
	
	protected def DisableScroller ():
		Offset = 0
		PausePacking()
		Remove(Scroller)
		Scroller = null
		ResumePacking()
	
	protected def Scroll (o as double):
		Offset = o
		Pack()
	
	Offset as double = 0
	
	override def AcceptsDrop (g as Gadget, x as double, y as double):
		return true
	
	override def Arrange ():
		c_x = Spacing
		c_y = Spacing - Offset
		max_h = 0.0
		max_x = Size.Width - Spacing
		max_x -= Scroller.MinSize.Width unless Scroller == null
		
		for i in Children:
			if i == Scroller:
				i.Rect=Rect(Size.Width-i.MinSize.Width, 0, Size.Width, Size.Height)
			else:
				// Needs new line?
				if c_x + i.MinSize.Width > max_x:
					c_x = Spacing
					c_y += max_h + Spacing
					max_h = 0.0
				
				i.Rect = Rect(c_x, c_y, c_x + i.MinSize.Width, c_y + i.MinSize.Height)
				c_x += i.MinSize.Width + Spacing
				max_h = System.Math.Max(max_h, i.MinSize.Height)
			
		if c_y + max_h > Size.Height - Spacing and Scroller == null:
			EnableScroller()
			Arrange()
		elif c_y + max_h + Offset <= Size.Height and Scroller != null:
			DisableScroller()
		
		unless Scroller == null:
			Scroller.RealSize = c_y + max_h + Offset - Size.Height

// FILER WINDOW
class FilerWindow (MainWindow):
	location as File
	Location as File:
		get:
			return location
		set:
			if value.HasContents:
				location = value
				TitleBar.Text = "${value.Name} - Filer"
				UpdateIcons()
			else:
				print "canny find ${value.Path}"
	
	//ToolBar as ButtonBar
	
	Icons as IconBox
	
	def constructor (dir as File):
		super("Filer")
		
		// Add icon box
		Icons = IconBox(self)
		
		Location = dir
		
		// Add some menu goodies
		Menu.AddItem("New Window", {FilerWindow(Location)})
		Menu.AddItem("Locations", "Home", {Location = File.Home})
		Menu.AddItem("Locations", "Up", {Location = Location.Parent})
		
	
	def UpdateIcons ():
		Icons.RemoveAll()
		
		TitleBar.Text = "Loading..."
		
		worker_thread = Thread() def ():
			files = Location.Contents
			
			icons = array(Icon, 0)
			for f in files:
				icons += (Icon(null, f, {Location = f}),) unless f.IsHidden
			
			EventLoop.Do() def ():
				for i in icons:
					Icons.Add(i)
			
			TitleBar.Text = "${Location.Name} - Filer"
			
		worker_thread.Start()

class FilerTool (UI.Tool, IOpen, IOpenFile):
	def Open ():
		FilerWindow(File.Home)
		
	def OpenFile (file as File):
		FilerWindow(file)
